package com.xbongbong.crm.abstracts;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.domain.entity.PaasFormExplainEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.CommonHelp;
import com.xbongbong.paas.lowcode.LowCodeConfigService;
import com.xbongbong.paas.model.PaasFormExplainModel;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.pojo.dto.FormDataAddGetDTO;
import com.xbongbong.paas.pojo.vo.FormDataAddGetVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.FormUtil;
import com.xbongbong.parent.abstracts.AbstractNewPageRenderService;
import com.xbongbong.parent.explain.FormDataAddFormExplainHandlerName;
import com.xbongbong.pro.enums.PackageTypeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.enums.teammembers.enums.FormOwnerUniqueEnum;
import com.xbongbong.pro.formexplain.pojo.dto.HandlerExplainDTO;
import com.xbongbong.pro.formexplain.pojo.vo.HandlerExplainVO;
import com.xbongbong.pro.lowcodeconfig.pojo.dto.LowCodeConfigGetDTO;
import com.xbongbong.pro.lowcodeconfig.pojo.vo.LowCodeConfigGetVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * crm业务父类，更改此处方法将影响所有继承该类的业务，请谨慎修改并review
 * @author 吴峰
 * @date 2022/01/18 19:28
 */
@Component
public abstract class AbstractCrmNewPageRenderService extends AbstractNewPageRenderService<PaasFormEntityExt> {

    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractCrmNewPageRenderService.class);

    @Resource
    private PaasFormModel paasFormModel;
    @Resource
    private PaasFormExplainModel paasFormExplainModel;
    @Resource
    private FormDataAddFormExplainHandlerName formDataAddFormExplainHandlerName;
    @Resource
    private LowCodeConfigService lowCodeConfigService;
    @Resource
    private CommonHelp commonHelp;

    @Override
    public LowCodeConfigGetVO getLowCodeConfig(LowCodeConfigGetDTO lowCodeConfigGetDTO) throws XbbException {
        // 获取低代码配置
        return lowCodeConfigService.get(lowCodeConfigGetDTO);
    }

    @Override
    public PaasFormEntityExt getForm(FormDataAddGetDTO formDataAddGetDTO) throws XbbException {
        Long formId = formDataAddGetDTO.getFormId();
        String corpid = formDataAddGetDTO.getCorpid();
        Integer businessType = formDataAddGetDTO.getBusinessType();
        PaasFormEntityExt paasFormEntityExt =  paasFormModel.getByKey(formId, corpid);
        if ( Objects.isNull(paasFormEntityExt) ) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100018, MessageConstant.FORM_NOT_EXIST);
        }
        if ( Objects.equals(paasFormEntityExt.getEnable(), 0) || Objects.equals(paasFormEntityExt.getDel(), 1) ) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100017);
        }
        paasFormModel.validateByPermission(paasFormEntityExt, formDataAddGetDTO.getLoginUser());
        FormUtil.handleFormEntity(paasFormEntityExt, paasFormEntityExt.getSaasMark(), businessType);
        return paasFormEntityExt;
    }

    @Override
    public List<FieldAttrEntity> getExplain(FormDataAddGetDTO formDataAddGetDTO) throws XbbException {
        Long formId = formDataAddGetDTO.getFormId();
        String corpid = formDataAddGetDTO.getCorpid();
        PaasFormExplainEntity paasFormExplainEntity = paasFormExplainModel.getByFormId(formId, corpid);
        if ( Objects.isNull(paasFormExplainEntity) ) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_EXPLAIN_NOT_EXIST);
        }
        Integer businessType = paasFormExplainEntity.getBusinessType();
        List<FieldAttrEntity> explainList = JSON.parseArray(paasFormExplainEntity.getExplains(), FieldAttrEntity.class);
        //根据套餐判断负责人属性
        Integer feeType = commonHelp.getCurrentCompanyPackage(corpid);
        boolean ultimateTag = Objects.equals(feeType, PackageTypeEnum.ULTIMATE.getType());
        if(!ultimateTag && FormOwnerUniqueEnum.getDefaultBusinessList().contains(businessType)){
            //只有旗舰版支持负责人唯一设置，其他版本拿默认设置
            FormOwnerUniqueEnum formOwnerUniqueEnum = FormOwnerUniqueEnum.getFormOwnerUniqueByBus(businessType);
            Integer multiple = formOwnerUniqueEnum.getValue();
            for (FieldAttrEntity fieldAttrEntity : explainList) {
                if(Objects.equals(fieldAttrEntity.getAttr(), FieldTypeEnum.OWNERID.getAlias())){
                    fieldAttrEntity.setMultiple(multiple);
                }
            }
        }
        return explainList;
    }


    @Override
    public HandlerExplainVO handleExplain(HandlerExplainDTO handlerExplainDTO, PaasFormEntityExt formEntityExt) throws XbbException {
        handlerExplainDTO.setPaasFormEntityExt(formEntityExt);
        return formDataAddFormExplainHandlerName.handleExplain(handlerExplainDTO);
    }


    @Override
    public void formatSaasAttrToReturn(FormDataAddGetDTO formDataAddGetDTO, FormDataAddGetVO formDataAddGetVO, PaasFormEntityExt paasFormEntityExt) throws XbbException {
        // 默认拼装
        formDataAddGetVO.setPaasFormEntity(paasFormEntityExt);
    }
}
